#pragma once

#include<iostream>
#include<unordered_map>
#include<sys/types.h>
#include<cstdlib>
#include<cstring>
#include<arpa/inet.h>
#include<sys/wait.h>
#include<sys/socket.h>
#include<netinet/in.h>
#include<unistd.h>
#include<pthread.h>
#include<signal.h>
#include"ThreadPool.hpp"
#include"Log.hpp"
#include"Init.hpp"

extern Log lg;
Init init;


class Task
{
public:
    Task(int sockfd,const std::string& clientip,const uint16_t& clientport)
    :sockfd_(sockfd),
    clientip_(clientip),
    clientport_(clientport)
    {}
    Task()
    {}
    void Run()
    {
        char buffer[4096];
        ssize_t n = read(sockfd_,buffer,sizeof(buffer));  //tcp是面向字节流的可以用read
        if(n > 0)
        {
            buffer[n] = 0;
            std::cout<<"client say# "<<buffer;
            std::string echo_string = init.translation(buffer);

            write(sockfd_,echo_string.c_str(),echo_string.size());
        }

        else if(n = 0)
        {
            lg(Info,"%s:%d quit,server close sockfd: %d",clientport_,clientip_.c_str(),sockfd_);
        }

        else
        {
            lg(Warning,"read error,sockfd: %d,client ip: %s,client port: %d",sockfd_,clientip_.c_str(),clientport_);
        }
        close(sockfd_);
    }
    void GetInstance()
    {}
    void operator()()
    {
        Run();
    }
    ~Task()
    {}
private:
    int sockfd_;
    std::string clientip_;
    uint16_t clientport_;
};